﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace KeDa2025_
{
    public partial class Master : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取session  取到头像和姓名 分别赋给image组件的imageurl和label.Text

            #region 管理员权限验证
            if (Session["User"] != null)
            {
                DataRow userRow = (DataRow)Session["User"];
                //查到登录用户的权限
                int manage = Convert.ToInt32(userRow["manager"]);
                if (manage == 0)
                {
                    lbtnUserManage.Visible = false;
                    liUserManager.Visible = false;
                }
            }
            #endregion
            DisplayUserInfo();

        }

        private void DisplayUserInfo()
        {
            // 假设 `Session["User"]` 存储的是 DataRow，先将其转换为 DataRow
            DataRow userRow = (DataRow)Session["User"];

            // 从 DataRow 中获取用户ID，假设用户ID列名为 "id"
            int userId = Convert.ToInt32(userRow["id"]);

            // 数据库查询以获取用户信息
            string sql = "SELECT account, img_url FROM users WHERE id = @userId";
            using (MySqlConnection conn = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;"))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@userId", userId);

                conn.Open();
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        // 将用户名绑定到 lbName 标签
                        lbName.Text = reader["account"].ToString();

                        // 将头像URL绑定到 Image1 控件
                        Image1.ImageUrl = reader["img_url"].ToString();
                    }
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            //1.清Session
            //2.删cookie
            Response.Redirect("denglu.aspx");
        }

        protected void lbClear_Click(object sender, EventArgs e)
        {
            // 清除所有 Session 数据
            Session.Clear();
            // 确保所有清除操作完成后，重定向到登录页面
            Response.Redirect("denglu.aspx");
        }

    }
}